const express = require('express');
let router = express.Router();

const mail=require("../../util/mail");
const sql = require('../../sql/sqlConfig');

router.post('/', function (req, res, next) {
    let email = req.body.email;
    let action = req.body.action;//1为注册2为修改密码
    let resetEmail=req.body.email+';flag=2';
    let reg = /^[a-zA-Z0-9]+([-_.][a-zA-Z0-9]+)*@[a-zA-Z0-9]+([-_.][a-zA-Z0-9]+)*\.[a-z]{2,}$/;
    if(!reg.test(email)) return res.send({errCode:9,message:'请输入正确的邮箱格式！'});
    //注册
    if(action==1){
        //判断验证码是否过期，过期了重新发送
        let verify = null;
        if(req.session[email]){ //判断session里面是否存在email这个key
            if((Date.parse(new Date()) - req.session[email][1])/1000 > 300){
                verify = Math.floor(Math.random()*(9999-1000))+1000;
                req.session[email] = [verify,Date.parse(new Date())];//存储session
            }else{
                return res.send({errCode:9,message:"验证码还未过期，请检查您的邮箱获取验证码!"});
            }
        }else{
            verify = Math.floor(Math.random()*(9999-1000))+1000;
            req.session[email] = [verify,Date.parse(new Date())];
        }

        let option = "select `id` from `user` where `email`='"+email+"'";
        sql.query(option, function (err, rows) {
            if (err) return res.send({errCode:-9999,message:'操作数据库失败'+err});
            if(rows.length!=0)
                //数据库有邮箱
                return res.send({errCode:9,message:'该邮箱已经注册过啦'});
            // 发送邮件
            let mailOptions = {
                from: '"分销商城" <2366060971@qq.com>', // 发送方邮箱
                to: email, // list of receivers
                subject: '[分销商城]', // Subject line
                html: "您的验证码是"+verify+",验证码有效期5分钟。" // html body
            };
            mail.transporter.sendMail(mailOptions, (error, info) => {
                if (error) return res.send({errCode:-9999,message:'发送验证码失败:'+error});
                return res.send({errCode:0,message:'发送验证码成功'})
            });
        });
    }
    //忘记密码
    else if(action==2){
        //判断验证码是否过期，过期了重新发送
        let verify = null;
        if(req.session[resetEmail]){
            if((Date.parse(new Date()) - req.session[resetEmail][1])/1000 > 300){
                verify = Math.floor(Math.random()*(9999-1000))+1000;
                req.session[resetEmail] = [verify,Date.parse(new Date())];//存储session
            }else{
                return res.send({errCode:9,message:"验证码还未过期，请检查您的邮箱获取验证码!"});
            }
        }else{
            verify = Math.floor(Math.random()*(9999-1000))+1000;
            req.session[resetEmail] = [verify,Date.parse(new Date())];
        }
        // console.log(verify,req.session[resetEmail]);

        let option = "select `id` from `user` where `email`='"+email+"'";
        sql.query(option, function (err, rows) {
            if (err) return res.send({errCode:-9999,message:'操作数据库失败'+err});
            if(rows.length==0)
                //数据库没有邮箱
                return res.send({errCode:9,message:'该邮箱还未注册'});
            // 发送邮件
            let mailOptions = {
                from: '"分销商城" <2366060971@qq.com>', // 发送方邮箱
                to: email, // list of receivers
                subject: '[分销商城]', // Subject line
                html: "您的验证码是"+verify+",验证码有效期5分钟。" // html body
            };
            mail.transporter.sendMail(mailOptions, (error, info) => {
                if (error) return res.send({errCode:-9999,message:'发送验证码失败:'+error});
                return res.send({errCode:0,message:'发送验证码成功'})
            });
        });
    }
});


module.exports = router;
